﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 彩虹岛乱码转换
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             
        }
         

        bool SetLocale(string type)
        {
            var startInfo = new ProcessStartInfo
            {
                FileName = "powershell.exe",
                Arguments = $"-Command \"Set-WinSystemLocale -SystemLocale {type}\"",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
            };
            using (var process = new Process())
            {
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                if (process.ExitCode == 0)
                {
                    textBox2.Text += ($"设置语言环境成功: {type}\r\n");
                    return true;
                }
                var error = process.StandardError.ReadToEnd();
                textBox2.Text += ($"设置语言环境失败: {error}\r\n");
                return false;
            }
        } 

        private void button2_Click(object sender, EventArgs e)
        {
            SetLocale("zh-TW");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SetLocale("zh-CN");
        }
    }
}
